package com.game.spacetrader.map;

import com.game.spacetrader.Game;
import com.game.spacetrader.Planet;
import com.game.spacetrader.R;
import com.game.spacetrader.data.Data;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.util.AttributeSet;
import android.view.Display;
import android.view.View;
import android.view.WindowManager;

import java.util.ArrayList;

/**
 * Makes the canvas for the game
 *
 */
public class GameCanvas extends View{

	private Bitmap mBitmap = null;
	private Paint mPaint = null;
	private double ratioX, ratioY;
	private ArrayList<Planet> planets = new ArrayList<Planet>();
	private int locx, locy;

	/**
	 * Constructor
	 * @param context
	 * @param aSet
	 */
	public GameCanvas(Context context, AttributeSet aSet) { 
		super(context, aSet); 
		//load our bitmap
		mBitmap = BitmapFactory.decodeResource(getResources(), R.drawable.planets);
		//create a paint brush
		mPaint = new Paint();
		mPaint.setColor(Color.BLACK);
		initPlanets();
		Coordinates c = Game.getInstance().getPlayer().getLocation();
		this.locx = c.getX();
		this.locy = c.getY();
		validPlanets();
		WindowManager wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
		Display display = wm.getDefaultDisplay();
		int width = display.getWidth();  // deprecated
		int height = display.getHeight();  // deprecated
		this.ratioX = width/100.0;
		this.ratioY = height/100.0;
	}

	/**
	 * Draws the canvas
	 */
	@Override
	public void onDraw(Canvas canvas) {
		//draw background
		//canvas.drawRect(0, 0, getWidth(), getHeight(), mPaint);
		Bitmap b = BitmapFactory.decodeResource(getResources(),R.drawable.map_b); 
		canvas.drawBitmap(b, 0,0, mPaint);
		//draw the Planets'
		for(Planet planet:planets)
			canvas.drawBitmap(mBitmap, (float)(planet.getX()*this.ratioX), (float)(planet.getY()*this.ratioY), null);
	}

	/**
	 * Retrieves all the planets and stores to arraylist<planets>
	 */
	public void initPlanets(){
		Data d = new Data("universe.db");
		Planet tempP;
		String tName, tSystem;
		String data;
		String[] data2;
		int x,y,tech,res;
		for(int i = 0; i < d.getNumRows(); i++){
			data = d.execSQL("SELECT * FROM @TABLE WHERE ID=" + i)[0];
			data2=data.split(" ");
			tName=data2[0];
			tSystem=data2[1];
			x=Integer.parseInt(data2[2]);
			y=Integer.parseInt(data2[3]);
			tech=Integer.parseInt(data2[4]);
			res=Integer.parseInt(data2[5]);
			tempP = new Planet(tName,tSystem,x,y,tech,res);
			planets.add(tempP);
		}
	}
	
	/**
	 * Gets all planets in range of gas
	 * @return
	 */
	private void validPlanets(){
		//get amount of gas here
		int gas = Game.getInstance().getPlayer().getGas();
		ArrayList<Integer> remove = new ArrayList<Integer>();
		int tempx, tempy;
		for(int i = 0; i < planets.size(); i++){
			tempx = this.locx - this.planets.get(i).getX();
			tempy = this.locy - this.planets.get(i).getY();
			if(gas < (int)(Math.sqrt((tempx*tempx + tempy*tempy))))
				remove.add(i);
		}
		for(int i = remove.size()-1; i >= 0; i--)
			this.planets.remove((int)(remove.get(i)));
	}
	
	public ArrayList<Planet> getPlanets(){
		return planets;
	}
	public double getRatioX(){
		return this.ratioX;
	}
	public double getRatioY(){
		return this.ratioY;
	}

}
